//
//  AppDelegate.m
//  英伦府邸
//
//  Created by IOS_DEVELOPER on 2017/7/27.
//  Copyright © 2017年 英伦府邸. All rights reserved.
//

#import "AppDelegate.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

@interface AppDelegate ()<CLLocationManagerDelegate>
@property (nonatomic,strong) MMDrawerController * drawerController;
/** CL管理者 */
@property (nonatomic, strong) CLLocationManager *clmgr;
@end

@implementation AppDelegate

- (CLLocationManager *)clmgr
{
    if (!_clmgr) {
        self.clmgr = [[CLLocationManager alloc] init];
    }
    return _clmgr;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // key board 0010
    [self setupKeyboard];
    self.window.backgroundColor = [UIColor whiteColor];
    // 初始化数据库
    [FMDBHelper setDataBaseName:kDBName];
    DLog(@"%@", [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:kDBName]);
    allConstMemberid = [USER_DEFAULTS objectForKey:@"user_id"];
    allConstMD5id = [USER_DEFAULTS objectForKey:@"verify"];
    allConstProjectid = [USER_DEFAULTS objectForKey:@"allConstProjectid"];
    if (allConstMemberid && allConstMD5id && allConstProjectid) {
        allConstNavImgName = [USER_DEFAULTS objectForKey:@"allConstNavImgName"];
        allConstHexColor = [USER_DEFAULTS objectForKey:@"allConstHexColor"];
        allConstDocPath = [USER_DEFAULTS objectForKey:@"allConstDocPath"];
        [self toMainVC];
    }

    [self OnceSend];
    appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    appBuild = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

    NSLog(@"版本号:%@\nbuild号:%@\n",appVersion,appBuild);
    // 定位
    //iOS8+
    if ([[UIDevice currentDevice].systemVersion doubleValue] > 8.0) {
        //申请用户的允许(只是前台)
        //Info.plist
        [self.clmgr requestWhenInUseAuthorization];
    }

    
    return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"applicationDidBecomeActive" object:nil];
    NSLog(@"---applicationDidBecomeActive----");
    //进入前台
    
    
}

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    if ([url.host isEqualToString:@"safepay"]) {
        // 支付跳转支付宝钱包进行支付，处理支付结果
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSLog(@"result = %@",resultDic);

        }];

    }
    return YES;
}

- (void)setupKeyboard
{
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    // 控制点击背景是否收起键盘
    manager.shouldResignOnTouchOutside = YES;
    // 控制键盘上的工具条文字颜色是否用户自定义
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    // 是否显示键盘上的工具条
    manager.enableAutoToolbar = YES;
    manager.enableAutoToolbar = YES;
    
    float sfOS = [Utils OSVersion];
    if (sfOS >=11) {
        manager.enableAutoToolbar = NO;
    }
    // 设置文本框的键盘距离,不能小于零,默认是10
    manager.keyboardDistanceFromTextField = 10;
}
- (void)toMainVC
{
        ZJTabBarController *vc = [[UIStoryboard storyboardWithName:@"tabBar" bundle:nil] instantiateInitialViewController];

        [UIView transitionWithView:[[UIApplication sharedApplication].delegate window]
                          duration:0.4
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{
                            BOOL oldState = [UIView areAnimationsEnabled];
                            [UIView setAnimationsEnabled:NO];
                            [[[UIApplication sharedApplication].delegate window] setRootViewController:vc];
                            [UIView setAnimationsEnabled:oldState];
                        } completion:NULL];

}
-(void)OnceSend{
    if (!allConstMemberid) {
        [ZJUserModel autoLogin];
    }

}
@end
